package com.codejiwei.core.graphx

import org.apache.spark.graphx.{Edge, Graph}
import org.apache.spark.{SparkConf, SparkContext}

object GraphXExample {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setMaster("local[1]").setAppName("GraphXExample")
    val sc = new SparkContext(conf)

    // 定义顶点和边
    val vertices = sc.parallelize(Seq((1L, "A"), (2L, "B"), (3L, "C"), (4L, "D")))
    val edges = sc.parallelize(Seq(Edge(1L, 2L, 1.0), Edge(2L, 3L, 1.0), Edge(3L, 4L, 1.0), Edge(4L, 1L, 1.0)))

    // 创建图
    val graph = Graph(vertices, edges)

    // 运行PageRank算法
    val pageRank = graph.pageRank(0.1).vertices
    val edges1 = graph.pageRank(0.1).edges

    // 打印结果
    pageRank.collect().foreach(println)
    edges1.collect().foreach(println)

    // 关闭SparkContext
    sc.stop()


  }
}
